import AntModal from '@/components/pt/dialog/AntModal'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import { addBizProject, updateBizProject } from '@/api/biz/bizProject'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import moment from 'moment';

export default {
  name: 'CreateForm',
  components: {
    AntModal,
    AdvanceTable
  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      labelCol: { span: 14 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      id: undefined,
      formTitle: '添加销售单',
      // 表单参数
      form: {},
      rules: {
        customerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
        orderType: [{ required: true, message: '订单类型不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('ok')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('ok')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,

        projectNumber: undefined,
        
        projectDate: undefined,

        remark: undefined

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.open = true
      this.formTitle = row.id ? '修改项目' : '新增项目';
      this.form = row
      if(row.id) {
        this.form.deptId = { ids: row.deptId, names: row.deptName }
      }
    },
    formatterTime(val){
      return val ? moment(val).format('YYYY-MM-DD') : ''
    },
    changeDate(row){
      row.projectDate = this.formatterTime(row.projectDate)
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updateBizProject(saveForm).then(response => {
              this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            })
          } else {
            addBizProject(saveForm).then(response => {
              this.$message.success('新增成功', 3)
                this.$emit('ok')
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizpurchase/index'
      this.$router.push(index)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length == 1
      this.multiple = selectedRowKeys.length
    },
    handleSubUpdate() {

    },
    handleSubDelete(row) {
      var that = this
      const productIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          let selectedList = JSON.parse(JSON.stringify(that.list));
          selectedList.forEach((item,index)=>{
            if(row.id) {
              that.list= that.list.filter(item => item.id!=row.id)
              return;
            } else {
              that.selectedRows.forEach(item2=>{
                that.list= that.list.filter(item => item.id!=item2.id)
                return;
              })
            }
          })
          that.$message.success(
            '删除成功',
            3
          )
          that.onSelectChange([], [])
          return;
        },
        onCancel () {}
      })
    },
    handleAddProduct() {
      this.showProductModal = true
      this.$nextTick(() => {
        this.$refs.bizProduct.handleAddProduct(this.list);
      })
    },
    selectProduct(selectedRows) {
      this.form.detailList = selectedRows;
      this.form.detailList.forEach(item=>{
        item.unitList.push({key:1,name:item.unit},{key:2,name:item.assistUnitA},{key:3,name:item.assistUnitB})
      })
    },
    handleChange(info) {
      if (info.file.status == 'done') {
       this.fileList=info.fileList;
      }
    },
    handleRemove(info) {
      this.fileList=this.fileList.filter(function(item) {
        return item.uid != info.uid
      })
    }

  }
}
